package com.mission.schedule.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.CommonDialog.LocalSynServiceDialog;
import com.mission.schedule.CommonDialog.ServerDataDownLocalServiceDialog;
import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.service.ClockService;
import com.mission.schedule.service.DownAPKService;
import com.mission.schedule.tools.toexcel.ToExcelActivity;
import com.mission.schedule.utils.ActivityManager1;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by liwei on 2017/5/15.
 */

public class AppSetActivity extends BaseActivity implements View.OnClickListener {

    TextView tixingname_tv;// , notification_tv
    TextView tixing_btn, help_btn;
    RelativeLayout tixing_ll, jifenduihuan_btn_ll;
    String untaskend;

    static App app = App.getDBcApplication();

    String userId;

    @ViewResId(id = R.id.bacl_ll)
    private LinearLayout bacl_ll;
//    @ViewResId(id = R.id.setbackground_tv)
//    private TextView setbackground_tv;// 设置背景
    @ViewResId(id = R.id.rl1)
    private RelativeLayout rl1;// 默认铃声
    @ViewResId(id = R.id.ringname_tv)
    private TextView ringname_tv;
    @ViewResId(id = R.id.morenbeforetime_tv)
    private TextView morenbeforetime_tv;// 默认提前提醒时间
    @ViewResId(id = R.id.morenbeforeday_tv)
    private TextView morenbeforeday_tv;// 默认到期日提醒时间
    @ViewResId(id = R.id.everydaywenhou_tv)
    private TextView everydaywenhou_tv;// 每天问候
    @ViewResId(id = R.id.alldaytixing_tv)
    private TextView alldaytixing_tv;// 全天的提醒时间
//    @ViewResId(id = R.id.yijianfankui_tv)
//    private TextView yijianfankui_tv;// 意见反馈
    @ViewResId(id = R.id.haoping_tv)
    private TextView haoping_tv;// 好评
    @ViewResId(id = R.id.aboutus_tv)
    private TextView aboutus_tv;// 关于我们
    @ViewResId(id = R.id.setendsound_tv)
    private TextView setendsound_tv;
    @ViewResId(id = R.id.updatedata_tv)
    private TextView updatedata_tv;
    @ViewResId(id = R.id.toggle_notifation)
    private ToggleButton toggle_notifation;
    @ViewResId(id = R.id.sun_rel)
    private RelativeLayout sun_rel;
    @ViewResId(id = R.id.todayend_ll)
    private RelativeLayout todayend_ll;
    @ViewResId(id = R.id.jifenqiandao_btn_ll)
    private RelativeLayout jifenqiandao_btn_ll;
    @ViewResId(id = R.id.status_bar_image)
    private RelativeLayout status_bar_image;
    @ViewResId(id = R.id.ll2)
    private LinearLayout ll2;
    @ViewResId(id = R.id.ll3)
    private LinearLayout ll3;
    @ViewResId(id = R.id.jf_ll)
    private LinearLayout jf_ll;

    @ViewResId(id = R.id.status_bar_text)
    private TextView status_bar_text;
    @ViewResId(id = R.id.jifen_today)
    private TextView jifen_today;
    @ViewResId(id = R.id.before_time)
    private TextView before_time;
    @ViewResId(id = R.id.sun_text)
    private TextView sun_text;
    @ViewResId(id = R.id.everyday_tv)
    private TextView everyday_tv;
    @ViewResId(id = R.id.allday_time)
    private TextView allday_time;
    @ViewResId(id = R.id.end_tv)
    private TextView end_tv;
    @ViewResId(id = R.id.tup)
    private TextView tup;
    @ViewResId(id = R.id.zongjifen)
    private TextView zongjifen;
    @ViewResId(id = R.id.daoqiri)
    private TextView daoqiri;
    @ViewResId(id = R.id.hidden_tv)
    private TextView hidden_tv;
    @ViewResId(id = R.id.udpateapp_tv)
    private TextView udpateapp_tv;
    @ViewResId(id = R.id.yijianfankui_tv)
    private TextView yijianfankui_tv;
    @ViewResId(id = R.id.cylxr_btn_ll)
    private RelativeLayout cylxr_btn_ll;
    @ViewResId(id = R.id.daiban_set)
    private TextView daiban_set;
    @ViewResId(id = R.id.daiban_set_tv)
    private TextView daiban_set_tv;


    Context context;
    // private final static int STATE_CHOOSE = 1;// 状态选择
    private static final int CHOOSE_MUSIC = 2;// 选择铃声
    private static final int BEFORETIME = 3;// 提前时间设置

    String ringcode;// 铃声对应的编码

    SharedPrefUtil sharedPrefUtil = null;
    String ringname;
    String beforetime;
    String qdrbefore_time;
    String isYouKe = "1";

    boolean isnotification = true;
    String notificationStr = "";
    ActivityManager1 activityManager = null;

    @Override
    protected void setListener() {
//        setbackground_tv.setOnClickListener(this);
        bacl_ll.setOnClickListener(this);
        rl1.setOnClickListener(this);
        ringname_tv.setOnClickListener(this);
        morenbeforetime_tv.setOnClickListener(this);
        morenbeforeday_tv.setOnClickListener(this);
        everydaywenhou_tv.setOnClickListener(this);
        alldaytixing_tv.setOnClickListener(this);
        ll2.setOnClickListener(this);
        ll2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ll3.setVisibility(View.VISIBLE);
                return true;
            }
        });
        ll3.setOnClickListener(this);
//        yijianfankui_tv.setOnClickListener(this);
        haoping_tv.setOnClickListener(this);
        aboutus_tv.setOnClickListener(this);
        setendsound_tv.setOnClickListener(this);
        updatedata_tv.setOnClickListener(this);
        jifenqiandao_btn_ll.setOnClickListener(this);
        sun_rel.setOnClickListener(this);
        status_bar_image.setOnClickListener(this);
        todayend_ll.setOnClickListener(this);
        udpateapp_tv.setOnClickListener(this);
        cylxr_btn_ll.setOnClickListener(this);
        yijianfankui_tv.setOnClickListener(this);
        daiban_set.setOnClickListener(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_appset_set);
        context = this;
        activityManager = ActivityManager1.getInstance();
        activityManager.addActivities(this);
    }
    File outfile;
    @Override
    protected void init(Bundle savedInstanceState) {

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userId = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERID, "0");
        isYouKe = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.ISYOUKE, "1");

        if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0") == null) {
            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0");
            sun_text.setText("关闭");
        } else {
            sun_text.setText(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0").equals("0") ? "关闭" : "打开");

        }

        ringcode = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MUSICCODE, "g_88");
        isYouKe = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, "1");
        beforetime = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.BEFORETIME, "0");
        qdrbefore_time = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.DQRBEFORETIME, "0");
        notificationStr = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNOTIFICATION, "0");
        if ("0".equals(notificationStr)) {
            status_bar_text.setText("显示");
        } else {
            status_bar_text.setText("隐藏");
        }

        tixing_btn = (TextView) this.findViewById(R.id.tixing_btn);
        tixing_btn.setOnClickListener(this);
        help_btn = (TextView) findViewById(R.id.help_btn);
        help_btn.setOnClickListener(this);
        jifenduihuan_btn_ll = (RelativeLayout) findViewById(R.id.jifenduihuan_btn_ll);
        jifenduihuan_btn_ll.setOnClickListener(this);
        tixing_ll = (RelativeLayout) findViewById(R.id.tixing_ll);
        tixing_ll.setOnClickListener(this);
        tixingname_tv = (TextView) findViewById(R.id.tixingname_tv);


        String ringstate = sharedPrefUtil.getString(context,
                ShareFile.USERFILE, ShareFile.RINGSTATE, "0");
        if (ringstate.equals("0")) {
            tixingname_tv.setText("语音");
            tixingname_tv.setTextColor(getResources().getColor(
                    R.color.mingtian_color));
            tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_tx), null, null, null);
        } else if (ringstate.equals("1")) {
            tixingname_tv.setText("静音");
            tixingname_tv.setTextColor(getResources().getColor(
                    R.color.sunday_txt));
            tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_jy), null, null, null);
        } else if (ringstate.equals("2")) {
            tixingname_tv.setText("短音");
            tixingname_tv.setTextColor(getResources().getColor(
                    R.color.mingtian_color));
            tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_tx), null, null, null);
        } else if (ringstate.equals("3")) {
            tixingname_tv.setText("振动");
            tixingname_tv.setTextColor(getResources().getColor(
                    R.color.mingtian_color));
            tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_zd), null, null, null);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onResume() {
        super.onResume();

        untaskend = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.UNTASKEND, "0");
        if ("0".equals(untaskend)) {
            hidden_tv.setText("隐藏");
        } else {
            hidden_tv.setText("显示");
        }
        String jifen = app.todayJifen();
        jifen_today.setText("今日积分:" + jifen);
        String zjf = sharedPrefUtil.getString(context,ShareFile.USERFILE,ShareFile.ZJF,"0");
        zongjifen.setText("总积分:"+zjf);
        int before = Integer.valueOf(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.BEFORETIME, "0"));
        String before_str = "0分钟";
        if (before == 0) {
            before_str = "0分钟";
        } else if (before == 5) {
            before_str = "5分钟";
        } else if (before == 15) {
            before_str = "15分钟";
        } else if (before == 30) {
            before_str = "30分钟";
        } else if (before == 60) {
            before_str = "1小时";
        } else if (before == 120) {
            before_str = "2小时";
        } else if (before == 24 * 60) {
            before_str = "1天";
        } else if (before == 48 * 60) {
            before_str = "2天";
        } else if (before == 7 * 24 * 60) {
            before_str = "1周";
        }
        before_time.setText(before_str);

        int dqr = Integer.valueOf(sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.DQRBEFORETIME, "0"));
        String dqr_str = "0天";
        if (dqr == 0) {
            dqr_str = "0天";
        } else if (dqr == 24 * 60) {
            dqr_str = "1天";
        } else if (dqr == 24 * 3 * 60) {
            dqr_str = "3天";
        } else if (dqr == 5 * 24 * 60) {
            dqr_str = "5天";
        } else if (dqr == 7 * 24 * 60) {
            dqr_str = "7天";
        } else if (dqr == 15 * 24 * 60) {
            dqr_str = "15天";
        }
        daoqiri.setText(dqr_str);

        ringname = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MUSICDESC, "完成任务");
        ringname_tv.setText(ringname);

        String morTime = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MORNINGTIME, "07:58");
        String nightTime = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.NIGHTTIME, "20:58");
        String morSta = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MORNINGSTATE, "0");
        String nigSta = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.NIGHTSTATE, "0");
        if (morSta.equals("0") && nigSta.equals("0") ){
            everyday_tv.setText(morTime + " " + nightTime);
        } else if (morSta.equals("0") && nigSta.equals("1")){
            everyday_tv.setText(morTime);
        } else if (morSta.equals("1") && nigSta.equals("0")){
            everyday_tv.setText(nightTime);
        } else {
            everyday_tv.setText("无");
        }

        String alldaysta = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ALLSTATE, "0");
        String alldaytime = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ALLTIME, "08:58");
        if (alldaysta.equals("0")){
            allday_time.setText(alldaytime);
        }else {
            allday_time.setText("无");
        }

        String duanyin = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ENDSOUNDSTATE, "0");
        String zhendong = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ENDWAKESTATE, "0");
        if (duanyin.equals("0") && zhendong.equals("0") ){
            end_tv.setText("短音 + 振动");
        } else if (duanyin.equals("0") && zhendong.equals("1")){
            end_tv.setText("短音");
        } else if (duanyin.equals("1") && zhendong.equals("0")){
            end_tv.setText("振动");
        } else {
            end_tv.setText("无");
        }

        String DBCOUNT_SHOW = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.DBCOUNT_SHOW, "4");
        daiban_set_tv.setText(DBCOUNT_SHOW+"");
    }
    private void VipDailog() {
        final android.app.AlertDialog builder = new android.app.AlertDialog.Builder(context).create();

        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setText("注册");
        delete_ok.setTextColor(Color.RED);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.dismiss();
                Intent newt = new Intent(context, PhoneCodeLoginActivity.class);
                newt.putExtra("type", 1);
                newt.putExtra("login", true);
                context.startActivity(newt);
            }
        });
        TextView cancle_tv = window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("忽略");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        TextView delete_tv = window.findViewById(R.id.delete_tv);


        delete_tv.setText("该功能是正式用户功能\n请注册正式用户");

    }
    @Override
    public void onClick(View v) {
        SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        Intent intent;
        switch (v.getId()) {
            case R.id.bacl_ll:
                onBackPressed();
                break;
            case R.id.udpateapp_tv:
                getAPPVersion();
                break;
            case R.id.status_bar_image:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNOTIFICATION, "0").equals("1")) {
                    // 选中
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISNOTIFICATION, "0");
                    status_bar_text.setText("显示");
                } else {
                    // 未选中
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISNOTIFICATION, "1");
                    status_bar_text.setText("隐藏");
                }
                intent = new Intent(this, ClockService.class);
                intent.setAction("com.mission.schedule.service.ClockService");
                intent.setPackage(getPackageName());
                intent.putExtra("WriteAlarmClockwrite", "0");
                startService(intent);
                break;
            case R.id.ll2:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    xiufudatadialog();
                } else {
                    Intent newt = new Intent(context, PhoneCodeLoginActivity.class);
                    newt.putExtra("type", 1);
                    newt.putExtra("login", true);
                    context.startActivity(newt);
                }
                break;
            case R.id.ll3:
                startActivity(new Intent(context, ToExcelActivity.class));
                break;
            case R.id.sun_rel:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE,
                        ShareFile.SHUNYANTYPE, "0").equals("0")) {
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.SHUNYANTYPE, "1");
                    sun_text.setText("打开");
                } else {
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.SHUNYANTYPE, "0");
                    sun_text.setText("关闭");
                }
                break;
            case R.id.common_set:
                startActivity(new Intent(context, AppSetActivity.class));
                break;
            case R.id.cylxr_btn_ll:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    Intent zhuanfa = new Intent(context, NewFriendsActivity.class);
                    zhuanfa.putExtra("titleid", "00");
                    startActivity(zhuanfa);
                } else {
                    VipDailog();
                }
                break;
            case R.id.jifenqiandao_btn_ll:
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    startActivity(new Intent(context, LocalQiandaoActivity.class));
                } else {
                    Intent newt = new Intent(context, PhoneCodeLoginActivity.class);
                    newt.putExtra("type", 1);
                    newt.putExtra("login", true);
                    context.startActivity(newt);
                }

                break;
            case R.id.jifenduihuan_btn_ll:// 积分兑换
                if (sharedPrefUtil.getString(context,
                        ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    startActivity(new Intent(context, JiFenDuiHuanActivity.class));
                } else {
                    Intent newt = new Intent(context, PhoneCodeLoginActivity.class);
                    newt.putExtra("type", 1);
                    newt.putExtra("login", true);
                    context.startActivity(newt);
                }
                break;
            case R.id.tixing_ll: // 提醒模式
                alterRingDialog();
                break;
            case R.id.help_btn:
                Intent intenth = new Intent(context, NoteOtherListActivity.class);
                intenth.putExtra("name", "常见问题");
                intenth.putExtra("puid", "5001");
                startActivity(intenth);
//                startActivity(new Intent(context, HelpActivity.class));
                break;
            case R.id.tixing_btn: // 提醒模式
                alterRingDialog();
                break;
            case R.id.todayend_ll:
                untaskend = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.UNTASKEND, "0");
                if ("0".equals(untaskend)) {
                    hidden_tv.setText("显示");
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.UNTASKEND, "1");
                } else {
                    hidden_tv.setText("隐藏");
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.UNTASKEND, "0");
                }
                Intent intentmain = new Intent(Const.SHUAXINDATA);
                intentmain.putExtra("data", "success");
                intentmain.putExtra("index", 1);
                intentmain.putExtra("what", 2);
                context.sendBroadcast(intentmain);
                Intent intent_app1 = new Intent();
                intent_app1.setAction("appwidget");
                intent_app1.putExtra("aappwidget1", true);
                context.sendBroadcast(intent_app1);
                break;
            case R.id.updatedata_tv:
                cancleDialog();
                break;
            case R.id.top_ll_back:
                break;
            case R.id.personmessage_tv:// 个人信息
                if ("1".equals(isYouKe)) {
                    startActivityForResult(new Intent(context, PersonMessageActivity.class), 999);
                } else {
                    Intent newt = new Intent(context, PhoneCodeLoginActivity.class);
                    newt.putExtra("type", 1);
                    newt.putExtra("login", true);
                    context.startActivity(newt);
                }
                break;
            case R.id.setbackground_tv:// 设置背景
                startActivityForResult(new Intent(context, SetBackgroundActivity.class), 999);
                break;
            case R.id.rl1:// 默认铃声
                intent = new Intent(context, LingShengActivity.class);
                intent.putExtra("schID", "");
                startActivityForResult(intent, CHOOSE_MUSIC);
                break;
            case R.id.ringname_tv:// 默认铃声
                intent = new Intent(context, LingShengActivity.class);
                intent.putExtra("schID", "");
                startActivityForResult(intent, CHOOSE_MUSIC);
                break;
            case R.id.morenbeforetime_tv:// 默认提前提醒时间""
                intent = new Intent(context, SetBeforeTimeActivity.class);
                intent.putExtra("time", beforetime);
                intent.putExtra("type", "time");
                startActivityForResult(intent, BEFORETIME);
                break;
            case R.id.morenbeforeday_tv:// 默认到期日提前前天
                intent = new Intent(context, SetBeforeTimeActivity.class);
                intent.putExtra("time", qdrbefore_time);
                intent.putExtra("type", "day");
                startActivityForResult(intent, BEFORETIME);
                break;
            case R.id.everydaywenhou_tv:// 每天问候时间
                startActivity(new Intent(context, EverydayTimeActivity.class));
                break;
            case R.id.alldaytixing_tv:// 全天的提醒时间
                startActivity(new Intent(context, AllDayTimeActivity.class));
                break;
            case R.id.yijianfankui_tv:// 意见反馈
                startActivity(new Intent(context, OpinionBackActivity.class));
                break;
            case R.id.haoping_tv:// 给个好评
                try {
                    Intent intent1 = new Intent(Intent.ACTION_VIEW);
                    intent1.setData(Uri.parse("market://details?id=" + context.getPackageName()));
                    startActivity(intent1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.aboutus_tv:// 关于我们
                startActivity(new Intent(context, AboutUsActivity.class));
                break;
            case R.id.setendsound_tv:// 设置结束的音效
                startActivity(new Intent(context, EndSoundActivity.class));
                break;
            case R.id.daiban_set:
                intent = new Intent(context, SetDaiBanActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void getAPPVersion() {
        final ProgressUtil progressUtil = new ProgressUtil();
        progressUtil.ShowProgress(context,true,true,"版本检测...");
        HttpUtils httpUtils = new HttpUtils(5000);
        httpUtils.send(HttpMethod.POST, URLConstants.获取最新版本号, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    Gson gson = new Gson();
                    MainActivity.AppVersionBean appversionbean = gson.fromJson(responseInfo.result, MainActivity.AppVersionBean.class);
                    if (appversionbean.status == 0) {
                        List<MainActivity.AppVersionBean.ListBean> list = appversionbean.list;
                        if (list != null) {
                            for (MainActivity.AppVersionBean.ListBean bean : list) {
                                if (Integer.valueOf(bean.android) > getVersionCode(Integer.valueOf(bean.android))) {
                                    updateApp(bean.android);
                                }else {
                                    Toast.makeText(context,"当前版本为最新版", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
                progressUtil.dismiss();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                progressUtil.dismiss();
            }
        });
    }
    public int getVersionCode(int code) {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return code;
        }
    }
    private void updateApp(final String version) {
        final AlertDialog builder = new AlertDialog.Builder(this).create();
        builder.setCancelable(false);
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);
        TextView cancle_tv = (TextView) window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("暂不更新");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        delete_tv.setText("已有新版本，请下载更新！");
        delete_ok.setText("立即更新");
        delete_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
                Intent intent = new Intent(context, DownAPKService.class);
                intent.setAction("DownAPK");
                startService(intent);
            }
        });
    }

    private void xiufudatadialog() {
        final Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_appset_shujuxiufu, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.show();

        more_pop_menu.findViewById(R.id.local).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                try {
                    if (app.QueryAllChongFuData(2).size() == 0 && app.queryAllSchData(-1, 0, 0).size() == 0 &&
                            App.getDBcApplication().getAllNoteTitlesData(userId, true).size() == 0 &&
                            App.getDBcApplication().getAllNoteDetailData(userId, true).size() == 0 &&
                            App.getDBcApplication().getAllNewFriendData(userId, true).size() == 0) {
                        Toast.makeText(context, "没有数据可以修复", Toast.LENGTH_SHORT).show();
                    } else {
                        LocalSynServiceDialog localSynServiceDialog = new LocalSynServiceDialog(context, R.style.dialog_translucent, getWindowManager());
                        localSynServiceDialog.show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        more_pop_menu.findViewById(R.id.network).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ServerDataDownLocalServiceDialog serverDataDownLocalServiceDialog = new ServerDataDownLocalServiceDialog(context, R.style.dialog_translucent, getWindowManager());
                serverDataDownLocalServiceDialog.show();
            }
        });
        more_pop_menu.findViewById(R.id.canel_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }

    /**
     * 设置提醒模式
     */
    private void alterRingDialog() {
        Dialog dialog = new Dialog(context, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(context);
        View more_pop_menu = fac.inflate(R.layout.dialog_setyuyin, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.show();

        new alterRingDialogOnClick(dialog, more_pop_menu);
    }

    class alterRingDialogOnClick implements View.OnClickListener {

        private View view;
        private Dialog dialog;
        private TextView yuyin_tv;
        private TextView jingyin_tv;
        private TextView duanyin_tv;
        private TextView zd_tv;
        private TextView canel_tv;

        @SuppressLint("NewApi")
        public alterRingDialogOnClick(Dialog dialog, View view) {
            this.dialog = dialog;
            this.view = view;
            initview();
        }

        private void initview() {
            yuyin_tv = (TextView) view.findViewById(R.id.yuyin_tv);
            yuyin_tv.setOnClickListener(this);
            zd_tv = (TextView) view.findViewById(R.id.zd_tv);
            zd_tv.setOnClickListener(this);
            jingyin_tv = (TextView) view.findViewById(R.id.jingyin_tv);
            jingyin_tv.setOnClickListener(this);
            duanyin_tv = (TextView) view.findViewById(R.id.duanyin_tv);
            duanyin_tv.setOnClickListener(this);
            canel_tv = (TextView) view.findViewById(R.id.canel_tv);
            canel_tv.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.yuyin_tv:
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.RINGSTATE, "0");
                    tixingname_tv.setText("语音");
                    tixingname_tv.setTextColor(getResources().getColor(
                            R.color.mingtian_color));
                    tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_tx), null, null, null);
                    dialog.dismiss();
                    break;
                case R.id.jingyin_tv:
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.RINGSTATE, "1");
                    tixingname_tv.setText("静音");
                    tixingname_tv.setTextColor(getResources().getColor(
                            R.color.sunday_txt));
                    tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_jy), null, null, null);
                    dialog.dismiss();
                    break;
                case R.id.duanyin_tv:
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.RINGSTATE, "2");
                    tixingname_tv.setText("短音");
                    tixingname_tv.setTextColor(getResources().getColor(
                            R.color.mingtian_color));
                    tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_tx), null, null, null);
                    dialog.dismiss();
                    break;
                case R.id.zd_tv:
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.RINGSTATE, "3");
                    tixingname_tv.setText("振动");
                    tixingname_tv.setTextColor(getResources().getColor(
                            R.color.mingtian_color));
                    tixing_btn.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.set_ring_zd), null, null, null);
                    dialog.dismiss();
                    break;
                case R.id.canel_tv:
                    dialog.dismiss();
                    break;
            }
        }
    }

//    private void ClearMAC() {
//        if (NetUtil.getConnectState(context) != NetUtil.NetWorkState.NONE) {
//            if (sharedPrefUtil.getString(context,
//                    ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "-1").equals("-1")) {
//                final String path = URLConstants.修改MAC地址 + userId + "&uClintAddr="
//                        + "" + "&uTocode=android";
//                StringRequest request = new StringRequest(Request.Method.GET, path,
//                        new Response.Listener<String>() {
//                            @Override
//                            public void onResponse(String s) {
//
//                            }
//                        }, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError volleyError) {
//                    }
//                });
//                request.setTag("down");
//                request.setRetryPolicy(new DefaultRetryPolicy(20000, 1, 1.0f));
//                App.getHttpQueues().add(request);
//            }
//        } else {
//            return;
//        }
//    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getHttpQueues().cancelAll("down");
    }

    private void cancleDialog() {
        final AlertDialog builder = new AlertDialog.Builder(this).create();
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_canclefocus);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.cancel();
                // App app = App.getDBcApplication();
                Intent intent = new Intent(context, SetDataUpdateActivity.class);
                File file = new File(
                        "/data/data/com.mission.schedule/databases/plan");
                File file1 = new File(Environment.getExternalStorageDirectory()
                        .getPath() + "/YourAppDataFolder/plan");
                // List<Map<String, String>> schList = new
                // ArrayList<Map<String,String>>();
                // List<Map<String, String>> repList = new
                // ArrayList<Map<String,String>>();
                // schList = app.queryAllSchData();
                // repList = app.QueryAllRepData();
                if (file.exists()) {
                    // if(schList!=null&&schList.size()>0){
                    // for(int i=0;i<schList.size();i++){
                    // app.deleteSchData(schList.get(i).get(ScheduleTable.schID));
                    // }
                    // }
                    // if(repList!=null&&repList.size()>0){
                    // for(int i=0;i<repList.size();i++){
                    // app.deleteRepData(repList.get(i).get(CLRepeatTable.repID));
                    // }
                    // }
                    intent.putExtra("type", "0");
                    startActivity(intent);
                } else if (file1.exists() && !file.exists()) {
                    // if(schList!=null&&schList.size()>0){
                    // for(int i=0;i<schList.size();i++){
                    // app.deleteSchData(schList.get(i).get(ScheduleTable.schID));
                    // }
                    // }
                    // if(repList!=null&&repList.size()>0){
                    // for(int i=0;i<repList.size();i++){
                    // app.deleteRepData(repList.get(i).get(CLRepeatTable.repID));
                    // }
                    // }
                    intent.putExtra("type", "1");
                    startActivity(intent);
                } else {
                    alertFailDialog();
                }
            }
        });
        TextView delete_canel = (TextView) window
                .findViewById(R.id.delete_canel);
        delete_canel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);
        delete_tv.setText("注意\n迁移数据时，所有本地数据将全部清空!");

    }

    private void alertFailDialog() {
        final AlertDialog builder = new AlertDialog.Builder(context).create();
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);
        window.setContentView(R.layout.dialog_alert_ok);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);
        delete_tv.setText("所需迁移的数据文件不存在！");
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });

    }

    @Override
    protected void setAdapter() {

    }
}
